package com.fxu.tpl.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fxu.framework.core.base.Cascade;
import com.fxu.framework.core.enums.EnumValid;
import com.fxu.framework.core.enums.StatusEnum;
import com.fxu.framework.core.sql.SEntity;
import com.fxu.tpl.enums.ColumnQueryTypeEnum;
import com.fxu.tpl.enums.ColumnTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

import static com.baomidou.mybatisplus.annotation.FieldFill.UPDATE;

/***
 * <p>fxu</p>
 * 字段表：[column]的数据实体类
 * @author fangxu
 * @version 2019-09-23
 ***/
@Data
@Builder
@AllArgsConstructor
@ApiModel(value = "Column", description = "字段")
@TableName("fxu_column")
public class Column extends SEntity<Column> implements Serializable {
/*=== 自动生成代码 开始 ===*/
    private static final long serialVersionUID = -4474123060448659144L;

    @TableId(type = IdType.AUTO)
    @ApiModelProperty(value = "自增ID")
    private Long id;

    @ApiModelProperty(value = "表ID", required = true)
    //@NotNull(message = "表ID必须赋值")
    private Long tableId;

    @ApiModelProperty(value = "字段名", required = true)
    @NotBlank(message = "字段名必须赋值")
    @Size(max = 64, message = "字段名的长度必须小于64位")
    private String name;

    @ApiModelProperty(value = "字段注释", required = true)
    @NotBlank(message = "字段注释必须赋值")
    @Size(max = 128, message = "字段注释的长度必须小于128位")
    private String cmmt;

    @ApiModelProperty(value = "字段类型[1:String,2:Boolean,3:Integer,4:Long,5:Float,6:Double,7:Decimal,8:Date,9:Text,10:Blob]", required = true)
    @NotNull(message = "字段类型必须赋值")
    @EnumValid(target = ColumnTypeEnum.class, message = "字段类型的值必须为{1:String,2:Boolean,3:Integer,4:Long,5:Float,6:Double,7:Decimal,8:Date,9:Text,10:Blob}")
    private Integer type;

    @ApiModelProperty(value = "字段长度", required = true)
    @NotNull(message = "字段长度必须赋值")
    private Integer length;

    @ApiModelProperty(value = "小数点前几位")
    private Integer dotLeft;

    @ApiModelProperty(value = "小数点后几位")
    private Integer dotRight;

    @ApiModelProperty(value = "默认值")
    @Size(max = 64, message = "默认值的长度必须小于64位")
    private String defVal;

    @ApiModelProperty(value = "关联表ID")
    private Long linkTableId;

    @ApiModelProperty(value = "关联保存")
    private Boolean linkSave;

    @ApiModelProperty(value = "是否唯一值")
    private Boolean isUnique;

    @ApiModelProperty(value = "是否能为notnull", required = true)
    @NotNull(message = "是否能为notnull必须赋值")
    private Boolean notNull;

    @TableField("`unsigned`")
    @ApiModelProperty(value = "是否无符号数", required = true)
    @NotNull(message = "是否无符号数必须赋值")
    private Boolean unsigned;

    @ApiModelProperty(value = "是否能为必填", required = true)
    @NotNull(message = "是否能为必填必须赋值")
    private Boolean required;

    @ApiModelProperty(value = "是否表单显示", required = true)
    @NotNull(message = "是否表单显示必须赋值")
    private Boolean formShow;

    @ApiModelProperty(value = "是否列表显示", required = true)
    @NotNull(message = "是否列表显示必须赋值")
    private Boolean listShow;

    @ApiModelProperty(value = "列表宽度")
    private Integer listWidth;

    @ApiModelProperty(value = "控件类型")
    private Integer formType;

    @ApiModelProperty(value = "控件宽度")
    private Integer formWidth;

    @ApiModelProperty(value = "查询类型[1:普通,2:模糊,3:后模糊,4:范围不含边界,5:范围含边界]")
    @EnumValid(target = ColumnQueryTypeEnum.class, message = "查询类型的值必须为{1:普通,2:模糊,3:后模糊,4:范围不含边界,5:范围含边界}")
    private Integer queryType;

    @ApiModelProperty(value = "验证函数")
    @Size(max = 64, message = "验证函数的长度必须小于64位")
    private String validFun;

    @ApiModelProperty(value = "最(小值/少个数)")
    private Integer validMin;

    @ApiModelProperty(value = "最(大值/多个数)")
    private Long validMax;

    @ApiModelProperty(value = "溢出缩短")
    private Boolean overflow;

    @ApiModelProperty(value = "限制集合")
    @Size(max = 2048, message = "限制集合的长度必须小于2048位")
    private String limits;

    @ApiModelProperty(value = "排序值", required = true)
    @NotNull(message = "排序值必须赋值")
    private Integer orderNo;

    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    @ApiModelProperty(value = "修改时间")
    private Date updateTime;

    @ApiModelProperty(value = "状态[1:正常,0:删除]")
    @EnumValid(target = StatusEnum.class, message = "状态的值必须为{1:正常,0:删除}")
    private Integer status;

    @TableField(fill = UPDATE, update = "%s+1")
    @ApiModelProperty(value = "版本号")
    private Integer version;

    @TableField(exist = false)
    @ApiModelProperty(value = "表对象")
    @Cascade(thisField = "tableId", linkField = "id")
    private Table table;

    @TableField(exist = false)
    @ApiModelProperty(value = "关联表对象")
    @Cascade(thisField = "linkTableId", linkField = "id")
    private Table linkTable;

    public String getTypeText() {
        return ColumnTypeEnum.desc(type);
    }
    public String getQueryTypeText() {
        return ColumnQueryTypeEnum.desc(queryType);
    }
    public String getStatusText() {
        return StatusEnum.desc(status);
    }
/*=== 自动生成代码 结束 ===*/
    
    public Column() {}

    @JsonIgnore
    public boolean isNumber() {
        return isInteger() || isLong() || isFloat() || isDouble();
    }
    @JsonIgnore
    public boolean isString() {
        return ColumnTypeEnum.String == ColumnTypeEnum.find(type)
                || ColumnTypeEnum.Text == ColumnTypeEnum.find(type)
                || ColumnTypeEnum.Blob == ColumnTypeEnum.find(type);
    }
    @JsonIgnore
    public boolean isBoolean() {
        return ColumnTypeEnum.Boolean == ColumnTypeEnum.find(type);
    }
    @JsonIgnore
    public boolean isInteger() {
        return ColumnTypeEnum.Integer == ColumnTypeEnum.find(type);
    }
    @JsonIgnore
    public boolean isLong() {
        return ColumnTypeEnum.Long == ColumnTypeEnum.find(type);
    }
    @JsonIgnore
    public boolean isFloat() {
        return ColumnTypeEnum.Float == ColumnTypeEnum.find(type);
    }
    @JsonIgnore
    public boolean isDouble() {
        return ColumnTypeEnum.Double == ColumnTypeEnum.find(type);
    }
    @JsonIgnore
    public boolean isDecimal() {
        return ColumnTypeEnum.Decimal == ColumnTypeEnum.find(type);
    }
    @JsonIgnore
    public boolean isDate() {
        return ColumnTypeEnum.Date == ColumnTypeEnum.find(type) || ColumnTypeEnum.DateTime == ColumnTypeEnum.find(type);
    }
    @JsonIgnore
    public boolean isText() {
        return ColumnTypeEnum.Text == ColumnTypeEnum.find(type) || ColumnTypeEnum.Blob == ColumnTypeEnum.find(type);
    }

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
		    return true;
        }
		if (obj == null) {
		    return false;
        }
		if (getClass() != obj.getClass()) {
		    return false;
        }
		Column other = (Column) obj;
		if (cmmt == null) {
			if (other.cmmt != null) {
			    return false;
            }
		} else if (!cmmt.equals(other.cmmt)) {
		    return false;
        }
		if (defVal == null) {
			if (other.defVal != null) {
			    return false;
            }
		} else if (!defVal.equals(other.defVal)) {
		    return false;
        }
		/*if (dotLeft == null) {
			if (other.dotLeft != null) {
			    return false;
            }
		} else if (!dotLeft.equals(other.dotLeft)) {
		    return false;
        }*/
		if (dotRight == null) {
			if (other.dotRight != null) {
			    return false;
            }
		} else if (!dotRight.equals(other.dotRight)) {
		    return false;
        }
		if (id == null) {
			if (other.id != null) {
			    return false;
            }
		} else if (!id.equals(other.id)) {
		    return false;
        }
		if (length == null) {
			if (other.length != null) {
			    return false;
            }
		} else if (!length.equals(other.length)) {
		    return false;
        }
		if (name == null) {
			if (other.name != null) {
			    return false;
            }
		} else if (!name.equals(other.name)) {
		    return false;
        }
		if (notNull == null) {
			if (other.notNull != null) {
			    return false;
            }
		} else if (!notNull.equals(other.notNull)) {
		    return false;
        }
		if (status == null) {
			if (other.status != null) {
			    return false;
            }
		} else if (!status.equals(other.status)) {
		    return false;
        }
		if (tableId == null) {
			if (other.tableId != null) {
			    return false;
            }
		} else if (!tableId.equals(other.tableId)) {
		    return false;
        }
		if (type == null) {
			if (other.type != null) {
			    return false;
            }
		} else if (!type.equals(other.type)) {
		    return false;
        }
		if (unsigned == null) {
			if (other.unsigned != null) {
			    return false;
            }
		} else if (!unsigned.equals(other.unsigned)) {
		    return false;
        }
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cmmt == null) ? 0 : cmmt.hashCode());
		result = prime * result + ((defVal == null) ? 0 : defVal.hashCode());
		// result = prime * result + ((dotLeft == null) ? 0 : dotLeft.hashCode());
		result = prime * result + ((dotRight == null) ? 0 : dotRight.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((length == null) ? 0 : length.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((notNull == null) ? 0 : notNull.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		result = prime * result + ((tableId == null) ? 0 : tableId.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result + ((unsigned == null) ? 0 : unsigned.hashCode());
		return result;
	}
}
